unit uSelectCust;

interface

uses
  WinApi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses,
  uniGUIClasses, uniGUIForm, uniEdit, uniLabel, uniButton, uniBitBtn,
  uniGUIBaseClasses, uniPanel, uniBasicGrid, uniDBGrid, Data.DB,
  Datasnap.DBClient, ClientRowSet, Vcl.Clipbrd;

type
  TSrvOrderModifyField = packed record
    sourceAccount: TField;
    custComp: TField;
    custMobile: TField;
    custName: TField;
    custAddr: TField;
  end;
  TFrmSelectCust = class(TUniForm)
    UniPanel1: TUniPanel;
    btSave: TUniBitBtn;
    UniLabel1: TUniLabel;
    edtcustNo: TUniEdit;
    btFind: TUniBitBtn;
    Grdcust: TUniDBGrid;
    crsCust: TClientRowSet;
    dsCust: TDataSource;
    crsCustCOMP_NAME: TStringField;
    crsCustXUNY_NO: TStringField;
    crsCustCITY: TStringField;
    crsCustADDRESS: TStringField;
    crsCustTEL: TStringField;
    btEdit: TUniBitBtn;
    crsCustDEFAULT_CONTACTOR: TStringField;
    pnlcustMobile: TUniPanel;
    UniLabel2: TUniLabel;
    edttel: TUniEdit;
    UniBitBtn1: TUniBitBtn;
    btClose: TUniBitBtn;
    crsCustCOMP_ID: TStringField;
    crsCustCARD_NO: TStringField;
    procedure GrdcustCellClick(Column: TUniDBGridColumn);
    procedure GrdcustColumnSort(Column: TUniDBGridColumn; Direction: Boolean);
    procedure GrdcustDblClick(Sender: TObject);
    procedure UniFormShow(Sender: TObject);
    procedure btSaveClick(Sender: TObject);
    procedure btFindClick(Sender: TObject);
    procedure btCloseClick(Sender: TObject);
    procedure btEditClick(Sender: TObject);
    procedure UniBitBtn1Click(Sender: TObject);
    procedure UniFormCreate(Sender: TObject);
  private
    { Private declarations }
    currField: TField;
    procedure SearchRecord;
  public
    { Public declarations }
    aType: Integer;
    soModifyField: TSrvOrderModifyField;
    accnoEdit: TUniEdit;
  end;

function FrmSelectCust: TFrmSelectCust;

implementation

{$R *.dfm}

uses
  MainModule, uniGUIApplication, ExpModule, uCommFunction;

function FrmSelectCust: TFrmSelectCust;
begin
  Result := TFrmSelectCust(UniMainModule.GetFormInstance(TFrmSelectCust));
end;

{ TFrmSelectCust }

procedure TFrmSelectCust.btCloseClick(Sender: TObject);
begin
  pnlcustMobile.Visible := False;
end;

procedure TFrmSelectCust.btEditClick(Sender: TObject);
begin
  if (not crsCust.Active) or (crsCust.RecordCount = 0) then
    Exit;
  edttel.Text := crsCust.FieldByName('tel').AsString;
  pnlcustMobile.Visible := True;
  pnlcustMobile.BringToFront;
end;

procedure TFrmSelectCust.btFindClick(Sender: TObject);
begin
  SearchRecord;
end;

procedure TFrmSelectCust.btSaveClick(Sender: TObject);
begin
  if (not crsCust.Active) or (crsCust.RecordCount = 0) then
    Exit;
  if aType = 0 then
  begin
    if soModifyField.sourceAccount <> nil then
      soModifyField.sourceAccount.AsString := crsCust.FieldByName('xuny_no').AsString;
    if soModifyField.custComp <> nil then
      soModifyField.custComp.AsString := crsCust.FieldByName('comp_name').AsString;
    if soModifyField.custMobile <>  nil then
      soModifyField.custMobile.AsString := crsCust.FieldByName('tel').AsString;
    if soModifyField.custName <> nil then
      soModifyField.custName.AsString := crsCust.FieldByName('default_contactor').AsString;
    if soModifyField.custAddr <> nil then
      soModifyField.custAddr.AsString := crsCust.FieldByName('address').AsString;
  end
  else
  if aType = 1 then
  begin
    if accnoEdit <> nil then
      accnoEdit.Text := crsCust.FieldByName('card_no').AsString;
  end;
  Close;
end;

procedure TFrmSelectCust.GrdcustCellClick(Column: TUniDBGridColumn);
begin
  currField := Column.Field;
end;

procedure TFrmSelectCust.GrdcustColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  SortColumn((Grdcust.DataSource.DataSet as TClientRowSet), Column.FieldName, Direction);
end;

procedure TFrmSelectCust.GrdcustDblClick(Sender: TObject);
begin
  if currField <> nil then
    Clipboard.AsText := currField.AsString;
end;

procedure TFrmSelectCust.SearchRecord;
begin
  crsCust.Close;
  crsCust.CommandText := 'SELECT comp_id,comp_name,xuny_no,city,address,tel,default_contactor,card_no FROM bas_company WHERE group_code=' + QuotedStr(dmExpress.UserInfo.GroupCode);
  if not dmExpress.UserInfo.IsCenter then
    crsCust.CommandText := crsCust.CommandText + ' AND sys_account=' + QuotedStr(dmExpress.UserInfo.SysAccount);
  if Trim(edtcustNo.Text) <> '' then
    crsCust.CommandText := crsCust.CommandText + ' AND (card_no=' + QuotedStr(edtcustNo.Text) +
      ' OR comp_name LIKE ' + QuotedStr('%' + edtcustNo.Text + '%') +
      ' OR tel=' + QuotedStr(edtcustNo.Text) + ')';
  crsCust.Open;
end;

procedure TFrmSelectCust.UniBitBtn1Click(Sender: TObject);
var
  SQLText: string;
begin
  crsCust.Edit;
  crsCust.FieldByName('tel').AsString := edttel.Text;
  crsCust.Post;

  SQLText := 'UPDATE bas_company SET tel=' + QuotedStr(edttel.Text) + ' WHERE comp_id=' + QuotedStr(crsCust.FieldByName('comp_id').AsString);
  dmExpress.ExecuteSQL(SQLText);

  pnlcustMobile.Visible := False;
end;

procedure TFrmSelectCust.UniFormCreate(Sender: TObject);
begin
  accnoEdit := nil;
  SetCompoentLanguage(Self);
end;

procedure TFrmSelectCust.UniFormShow(Sender: TObject);
begin
  SearchRecord;
  SetFieldsSort(Grdcust);
  currField := nil;

  btEdit.Visible := aType = 0;
end;

end.
